// pages/details/details.js

const app = getApp()

var util = require('../../utils/util.js')

Page({
  //  页面的初始数据
  data: {
    checkpoint: 0,
    musicStart: "end",
    imageStart: "imageEnd",
    playStart: "0",
    CountDown: 20,
    Correct: true,
    error: true,
    overtime: true,
    option: [],
    id: "",
    music: "",
    musicNumber: -1,
    examData: [],
    musicUrl: '',
    avdUrl1: 'http://p9ofir1eb.bkt.clouddn.com/image/pc_02.jpg',
    avdUrl2: 'http://p9ofir1eb.bkt.clouddn.com/image/pc_02.jpg',
    avdUrl3: 'http://p9ofir1eb.bkt.clouddn.com/image/pc_02.jpg',
    waveMove: 0,
    isGZ: 1,
    isADV: 1,
    shareNum: 3,
    shareNumst: 3,
    ushareNum: 3,
    errorSound:[],
    ShareError: true,
    is_Share:false,
    unshare:false
  },
  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    var that = this;

    if (app.globalData.userInfo) {
      // var shareNumber = "shareNumber"+ app.globalData.userInfo.id

      wx.getStorage({
        key: 'shareObj',
        success: function (res) {
          // shareNum: res.data.shareTime,
            //console.log(res.data)
          var shareObj = JSON.parse(res.data)
          that.setData({
            shareNum: shareObj.shareTime,
            shareNumst: shareObj.shareTime
          })
        }
      })
    } else {
      // wx.getStorage({
      //     key: 'ushareNum',
      //     success: function(res) {
      //         console.log(res)
      //         that.setData({
      //             shareNum:res.data,
      //             shareNumst:res.data
      //         })
      //     },
      // })
    }
    that.getOnlineExam()
    wx.request({
      url: 'https://caige1.sanliwenhua.com/switch',
      method: "get",
      success: function (res) {
          //console.log(res, "开关")
        if (res.data.code = 200) {
          var isADV = res.data.data.isADV;
          var isGZ = res.data.data.isGZ
          that.setData({//开关数据固定
            isGZ: isGZ,
            isADV: isADV
          })
        } else {

        }
      }
    })
    that.onloadsong()
    wx.request({
      url: 'https://caige1.sanliwenhua.com/all_advs',
      method: "get",
      success: function (res) {
          //console.log(res, "广告")
        if (res.data.code = 200) {
          var AdvData = res.data.data;
          that.setData({
            avdUrl1: AdvData[0].pic,
            avdUrl2: AdvData[1].pic,
            avdUrl3: AdvData[2].pic
          })
        }
      }
    })
    that.waveAnimation()
  },

  onloadsong: function () {
    var that = this;
    that.setData({
      musicNumber: that.data.musicNumber + 1,
      checkpoint: that.data.checkpoint + 1
    })
    if (!app.globalData.userInfo) {
      app.globalData.userInfo = {}
    }
    app.globalData.userInfo.checkpoint = that.data.checkpoint;
    var CountDown = that.data.CountDown;
    var musicNumber = []
    musicNumber[1] = that.data.musicNumber % 10
    musicNumber[0] = parseInt(that.data.musicNumber / 10)
      //console.log(musicNumber)
      //console.log(musicNumber[1], musicNumber[0])
    function randomSort(a, b) {
      return Math.random() > 0.5 ? -1 : 1;
    }
    var a = musicNumber[0];
    var b = musicNumber[1];
    var option = [];
    for (var y = 0; y < that.examData[a][b].err_ans.length; y++) {
      option.push(that.examData[a][b].err_ans[y])
    }
    var id = that.examData[a][b].id;
    var music = that.examData[a][b].music;
    var Correctmusic = { id: id, music: music }
    option.push(Correctmusic)
      //console.log(option)
    option = option.sort(randomSort);
      //console.log(option)
    that.setData({
      option: option,
      id: id,
      music: music
    })
    // app.audioCtx = wx.createInnerAudioContext()
    // app.audioCtx.loop = true
    // app.audioCtx.autoplay = true
    // app.audioCtx.src = 'https://res7.sanliwenhua.com/' + that.examData[a][b].url;

      //console.log(333333, JSON.stringify(app.audioCtx))
    that.setData({
      musicUrl: 'https://res7.sanliwenhua.com/' + that.examData[a][b].url
    })
    var audioCanPlayFun = function () {
      that.setData({
        musicStart: "stat",
        imageStart: "imageStat",
        playStart: "1",
      })
      clearInterval(that.interval)
      that.countDownFun()
    }
    that.resetSonunContext('https://res7.sanliwenhua.com/' + that.examData[a][b].url, audioCanPlayFun)
    // app.audioCtx.onEnded(() => {
    //   that.setData({
    //     musicStart: "end",
    //     imageStart: "imageEnd",
    //     playStart: "0"
    //   })
    // })
    // app.audioCtx.onCanplay(function () {

    //   // that.interval = setInterval(function () {
    //   //   CountDown = CountDown - 1;
    //   //   that.setData({
    //   //     CountDown: CountDown,
    //   //   })
    //   //   if (CountDown == 0) {
    //   //     clearInterval(that.interval)
    //   //     app.audioCtx.pause()
    //   //     that.setData({
    //   //       musicStart: "end",
    //   //       imageStart: "imageEnd",
    //   //       playStart: "0",
    //   //       overtime: false
    //   //     })
    //   //   }
    //   // }, 1000)
    // })
    // app.audioCtx.play()

  },
  //倒计时
  countDownFun: function () {
    var that = this;
    console.log
    var CountDown = that.data.CountDown
    that.interval = setInterval(function () {
      CountDown = CountDown - 1;
      that.setData({
        CountDown: CountDown,
      })
      if (CountDown <= 0) {
        clearInterval(that.interval)
        app.audioCtx.pause()
        that.setData({
          musicStart: "end",
          imageStart: "imageEnd",
          playStart: "0",
          overtime: false
        })
      }
    }, 1000)
  },
  //暂停倒计时
  stopCountDownFun: function () {
    clearInterval(this.interval)
  },
  //歌曲选择
  optionBindtap: function (e) {
    var that = this;
    clearInterval(that.interval)
    app.audioCtx.pause()
    that.setData({
      musicStart: "end",
      imageStart: "imageEnd",
      playStart: "0",
    })
    if (e.currentTarget.dataset.id == that.data.id) {
      that.updata()
      that.setData({
        Correct: false
      })
    } else {
      that.setData({
        error: false
      })
    }
  },
  updata: function () {
    var that = this;
      //console.log('ready toUpdata')
      //console.log(app.globalData)
    if (app.globalData.userInfo) {
        //console.log('toUpdata')
      var NewTime = new Date().getTime().toString();
        //console.log(NewTime, app.globalData.userInfo.id)
      var key = util.sha1(app.globalData.userInfo.id + NewTime + app.globalData.userInfo.checkpoint + app.globalData.userInfo.id);
        //console.log(key)
      key = key.toUpperCase()
      var data = {}
        //console.log(app.globalData)
      if (app.globalData.Sharing) {
        data = {
          key: key,
          time: NewTime,
          checkpoint: app.globalData.userInfo.checkpoint,
          x_token: app.globalData.userInfo.token,
          errMsg: app.globalData.Sharing.errMsg,
          encryptedData: app.globalData.Sharing.encryptedData,
          iv: app.globalData.Sharing.iv,
          session_key: app.globalData.session_key
        }
      } else {
        data = {
          key: key,
          time: NewTime,
          checkpoint: app.globalData.userInfo.checkpoint,
          x_token: app.globalData.userInfo.token,
        }
      }
      wx.request({
        url: 'https://caige1.sanliwenhua.com/update_checkpoint',
        method: "post",
        data: data,
        success: function (res1) {
            //console.log("我要获取group_id",res1)
          app.globalData.group_id = res1.data.group_id
          if (res1.data.data) {
            /*wx.setStorage({
              key: 'checkpoint',
              data: res.data.data.checkpoint
            })*/
              wx.getStorage({
                  key: 'userInfo',
                  success: function (res) {
                      //console.log(res.data)
                      var userInfoLocal = {}
                      userInfoLocal = JSON.parse(res.data)
                      //console.log(userInfoLocal)
                      userInfoLocal.group_id = res1.data.group_id;
                      userInfoLocal.checkpoint = res1.data.data.checkpoint;
                      wx.setStorage({
                          key: 'userInfo',
                          data: JSON.stringify(userInfoLocal)
                      })
                  },
                  fail: function () {

                  }
              })
          }

        },
        fail: function (res) {

        }
      })
    }
  },
  //返回首页
  navigateBackFunc: function () {
    var that = this;
      //console.log(app.globalData.userInfo)
    app.audioCtx.pause()
    wx.stopVoice()
    // that.sound = http://p9ofir1eb.bkt.clouddn.com/empty.mp3
    var backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.onStop()
    that.sound = wx.createInnerAudioContext()

    that.sound.src = 'http://p9ofir1eb.bkt.clouddn.com/empty.mp3';
    that.sound.autoplay = true
    app.audioCtx.pause()
    app.audioCtx.stop()
    /*if (app.globalData.userInfo) {
      var NewTime = new Date().getTime().toString();
      var key = util.sha1(app.globalData.userInfo.id + NewTime + app.globalData.userInfo.id);
      console.log(key)
      key = key.toUpperCase()
      wx.request({
        url: 'https://caige1.sanliwenhua.com/update_checkpoint',
        method: "post",
        data: {
            key: key,
            time: NewTime,
            checkpoint: that.data.checkpoint,
            x_token: app.globalData.userInfo.token,
        },
        success: function (res) {
          console.log(res)
        },
        fail: function (res) {

        }
      })
    }*/
    // wx.redirectTo({
    //   url: '../index/index'
    // })
    wx.stopVoice()
    // that.sound = http://p9ofir1eb.bkt.clouddn.com/empty.mp3
    var backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.onStop()
    that.sound = wx.createInnerAudioContext()
    that.sound.src = 'http://p9ofir1eb.bkt.clouddn.com/empty.mp3';
    that.sound.autoplay = true
    app.audioCtx.pause()
    app.audioCtx.stop()
    wx.navigateBack()

  },
  //下一首
  autoNext: function () {
    var that = this;
    setInterval(function () {
      that.PromptKey()
    }, 1000)
  },
  PromptKey: function () {
    var that = this;
    that.setData({
      musicStart: "stat",
      imageStart: "imageStat",
      playStart: "1",
      Correct: true,
      CountDown: 20,
    })
    app.audioCtx.pause()
    that.onloadsong()
  },
  //重新开始
  PromptSecond: function () {
    var that = this;
    app.audioCtx.pause()
    that.setData({
      musicStart: "stat",
      imageStart: "imageStat",
      playStart: "1",
      // musicNumber: -1,
      error: true,
      overtime: true,
      CountDown: 20,
      checkpoint: 0,
      ShareError:true,
      is_Share:false
    })
    that.onloadsong()
  },
  //歌曲结束
  Playended: function () {
    var that = this;
    that.setData({
      musicStart: "end",
      imageStart: "imageEnd",
      playStart: "0"
    })
  },
  //播放
  audioPlay: function () {
    // app.audioCtx.seek(0)
    app.audioCtx.play()
    this.countDownFun()
    this.setData({
      musicStart: "stat",
      imageStart: "imageStat",
      playStart: "1"
    })
  },
  //停止播放
  audioPause: function () {
    app.audioCtx.pause()
    var that = this;
    that.stopCountDownFun()
    that.setData({
      musicStart: "end",
      imageStart: "imageEnd",
      playStart: "0"
    })
  },

  // 生命周期函数--监听页面初次渲染完成
  onReady: function () {

  },
// 用户点击右上角分享
    onShareAppMessage: function (res) {
        var that = this;
        //console.log(that)
        // _this.data.pic = 'http://p4svid6ld.bkt.clouddn.com/image/zhuanbi.jpg'; //测试修改
        //app.globalData.CountDown = that.data.CountDown

        if (res.from === 'button') {
            //console.log(res)//.target
            that.setData({
                is_Share:true
            })
            return {
                title: '[@我]想要与您进行猜歌挑战，点击进入！',
                path: 'pages/index/index',
                //imageUrl: '/image/pc_03.jpg',
                success: function (res) {
                    wx.showToast({
                        title: '分享成功!',
                        icon: 'success',
                        duration: 2000
                    })
                    console.log("button分享到群的标记",res.shareTickets)
                    if(res.shareTickets){
                        that.fromShareBack = true;
                        console.log('分享到群的标记', res, res.shareTickets[0])
                        app.globalData.shareTicket = res.shareTickets[0]
                        var shareNum = that.data.shareNum - 1
                        var shareNumst = that.data.shareNumst - 1
                        if (app.globalData.userInfo) {
                            var shareNumber = "shareNumber" + app.globalData.userInfo.id
                            var mytime = new Date()
                            var getFullYear = mytime.getFullYear().toString()
                            var getMonth = (mytime.getMonth() + 1).toString()
                            if (getMonth < 10) {
                                getMonth = "0" + (mytime.getMonth() + 1).toString()
                            }
                            var getDate = mytime.getDate().toString()
                            if (getDate < 10) {
                                getDate = "0" + (mytime.getDate()).toString()
                            }
                            var date = getFullYear + getMonth + getDate
                            //console.log(date)
                            /*var shareNumber = "shareNumber" + app.globalData.userInfo.id
                            var sharedata = 'usreid:'+app.globalData.userInfo.id+',shareNum:'+shareNum+',time:'+date*/
                            //console.log("最新存储",sharedata)
                            if (shareNumst < 0) {
                                shareNum = 0
                                that.setData({
                                    shareNum: shareNum,
                                    shareNumst: shareNumst
                                })
                                app.globalData.shareObj.shareTime = shareNum;
                                wx.setStorage({
                                    key: 'shareObj',
                                    data: JSON.stringify(app.globalData.shareObj)
                                })
                            } else {
                                app.globalData.shareObj.shareTime = shareNum;
                                wx.setStorage({
                                    key: 'shareObj',
                                    data: JSON.stringify(app.globalData.shareObj)
                                })
                                that.setData({
                                    shareNum: shareNum,
                                    shareNumst: shareNumst
                                })
                                wx.setStorage({
                                    key: 'shareNum',
                                    data: shareNum
                                })
                            }
                            wx.setStorage({
                                key: 'time',
                                data: date
                            })
                        }
                        that.setData({
                            error: true,
                            overtime: true,
                            ShareError:true,
                            CountDown: 20,
                        })
                    }else{
                        that.setData({
                            error: true,
                            overtime: true,
                            ShareError:true,
                            CountDown: 20,
                        })
                        if(that.data.shareNum> 0){
                            wx.showModal({
                                title: '提示',
                                content: '分享到微信群才有复活机会哦！',
                                success: function(res) {
                                    if (res.confirm) {
                                        console.log('用户点击确定')
                                        that.setData({
                                            error: false,
                                            is_Share:false
                                        })
                                    } else if (res.cancel) {
                                        console.log('用户点击取消')
                                        wx.navigateBack()
                                    }
                                }
                            })
                        }else{
                            wx.navigateBack()
                        }
                    }
                    if(app.globalData.platform=="android"){
                        console.log("我是安卓才执行")
                        that.chuli();
                    }
                },
                fail: function (res) {
                    wx.showToast({
                        title: '分享失败！',
                        image: '/image/errIcon.png',
                        duration: 2000
                    })
                }
            }
        }else {
            // clearInterval(that.interval)
            console.log("2我是第几秒",that.data.CountDown)
            return {
                title: '[@我]想要与您进行猜歌挑战，点击进入！',
                path: 'pages/index/index',
                //imageUrl: '/image/pc_03.jpg',
                success: function (res) {
                    console.log('分享到群的标记',res.shareTickets)
                    console.log("3我是第几秒",that.data.CountDown)
                    wx.showToast({
                        title: '分享成功!',
                        icon: 'success',
                        duration: 2000
                    })
                    if(res.shareTickets){
                        console.log('分享到群的标记', res, res.shareTickets[0])
                        app.globalData.shareTicket = res.shareTickets[0]
                        that.fromShareBack = true;
                    }else{
                        that.setData({
                            unshare:true
                        })
                        if(app.globalData.platform =="android"){
                            console.log("没有分享到群,我是安卓才执行")
                            that.unfromShareBback();
                        }
                    }
                    if(app.globalData.platform =="android"){
                        console.log("我是安卓才执行")
                        that.chuli();
                    }
                },
                fail: function (res) {
                wx.showToast({
                    title: '分享失败！',
                    image: '/image/errIcon.png',
                    duration: 2000
                })
            }
            }
        }

    },
    unfromShareBback:function () {

        var that = this;
        console.log("我是第几秒",that.data.CountDown)
        that.setData({
            musicStart: "stat",
            imageStart: "imageStat",
            playStart: "1",
            error: true,
            overtime: true,
            ShareError:true,
            CountDown: 20,
        })
        var onCanplayfun = function () {
            var CountDown = that.data.CountDown;
            clearInterval(that.interval)
            that.interval = setInterval(function () {
                CountDown--;
                that.setData({
                    CountDown: CountDown,
                })
                console.log(CountDown)
                if (CountDown <= 0) {
                    clearInterval(that.interval)
                    //console.log()
                    app.audioCtx.pause()
                    that.setData({
                        musicStart: "end",
                        imageStart: "imageEnd",
                        playStart: "0",
                        overtime: false
                    })
                }
            }, 1000)
        }
        that.resetSonunContext(that.data.musicUrl, onCanplayfun)

    },
  // 生命周期函数--监听页面显示
  onShow: function () {
      wx.showShareMenu({
          withShareTicket: true
      })
      if(app.globalData.platform=="ios"){
          console.log("我是苹果才执行")
          this.chuli();
      }
      if(app.globalData.platform =="ios"&&this.data.unshare){
          console.log("没有分享到群,我是ios才执行")
          this.unfromShareBback();
      }
  },
    chuli:function () {
        app.globalData.isDetailPage = true;
        var that = this;
        //console.log(NewTime).log(5555, this.fromShareBack)
        that.waveAnimation()
        //如果是从分享成功进来的
        console.log("进入分享解码存储",that.fromShareBack,app.globalData.shareTicket)
        if (that.fromShareBack) {
            wx.getShareInfo({
                shareTicket: app.globalData.shareTicket,
                success: function (res) {
                    //console.log(res)
                    app.globalData.Sharing = {}
                    app.globalData.Sharing.errMsg = res.errMsg
                    app.globalData.Sharing.encryptedData = res.encryptedData
                    app.globalData.Sharing.iv = res.iv
                    that.updata()
                }
            })
            that.fromShareBack = false;
            console.log("是否按钮分享",that.data.is_Share)
            if(that.data.is_Share){
                that.setData({
                    is_Share:false
                })
                if (that.data.shareNumst >= 0) {
                    /*app.audioCtx = wx.createInnerAudioContext()
                    app.audioCtx.loop = true
                    app.audioCtx.src = that.data.musicUrl;
                    app.audioCtx.onError (function () {
                      console.log('音频意外报错2')
                      app.audioCtx = wx.createInnerAudioContext()
                      app.audioCtx.onPause(function () {
                        console.log('歌曲暂停播放')
                      })
                      app.audioCtx.loop = true
                      app.audioCtx.autoplay = true
                      app.audioCtx.src = that.data.musicUrl;
                      app.audioCtx.onCanplay(function () {
                        that.setData({
                          musicStart: "stat",
                          imageStart: "imageStat",
                          playStart: "1",
                        })
                        clearInterval(that.interval)
                        that.countDownFun()
                      })
                    })
                    app.audioCtx.autoplay = true*/
                    that.data.CountDown = 20;
                    that.setData({
                        musicStart: "stat",
                        imageStart: "imageStat",
                        playStart: "1",
                        ShareError:true,
                        error: true,
                        overtime: true,
                        CountDown: 20,
                    })
                    var onCanplayfun = function () {

                        var CountDown = that.data.CountDown;
                        clearInterval(that.interval)
                        that.interval = setInterval(function () {
                            CountDown--;
                            that.setData({
                                CountDown: CountDown,
                            })
                            console.log(CountDown)
                            if (CountDown <= 0) {
                                clearInterval(that.interval)
                                //console.log()
                                app.audioCtx.pause()
                                that.setData({
                                    musicStart: "end",
                                    imageStart: "imageEnd",
                                    playStart: "0",
                                    overtime: false
                                })
                            }
                        }, 1000)
                    }
                    that.resetSonunContext(that.data.musicUrl, onCanplayfun)
                    // app.audioCtx.onCanplay(() => {
                    //   onCanplayfun
                    // })
                } else {
                    // wx.redirectTo({
                    //   url: '../index/index'
                    // })
                    app.audioCtx.pause()
                    app.audioCtx.stop()
                    app.audioCtx.stop()
                    wx.navigateBack()
                }
            }else {
                console.log("不是按钮分享到这里")
                that.data.CountDown = 20;
                that.setData({
                    musicStart: "stat",
                    imageStart: "imageStat",
                    playStart: "1",
                    ShareError:true,
                    error: true,
                    overtime: true,
                    CountDown: 20,
                })
                var onCanplayfun = function () {
                    var CountDown = that.data.CountDown;
                    clearInterval(that.interval)
                    that.interval = setInterval(function () {
                        CountDown--;
                        that.setData({
                            CountDown: CountDown,
                        })
                        console.log(CountDown)
                        if (CountDown <= 0) {
                            clearInterval(that.interval)
                            //console.log()
                            app.audioCtx.pause()
                            that.setData({
                                musicStart: "end",
                                imageStart: "imageEnd",
                                playStart: "0",
                                overtime: false
                            })
                        }
                    }, 1000)
                }
                that.resetSonunContext(that.data.musicUrl, onCanplayfun)
            }

        }

    },
  resetSonunContext: function (url, onCanplayFun) {
    console.log("我已经到了播放的方法了")
    var that = this;
    app.audioCtx = wx.createInnerAudioContext()
    console.log("我已经到了播放的方法了2")
    app.audioCtx.loop = true
    app.audioCtx.onPlay(function () {
      console.log('歌曲开始播放')
    })
    app.audioCtx.onPause(function () {
      console.log('歌曲暂停播放')
      console.log(that.data)
      if (app.globalData.isDetailPage == true & that.data.playStart == '1') {
        app.audioCtx.play()
        var soundName = that.data.musicUrl.replace('https://res7.sanliwenhua.com/','')
        var data = {sound:soundName,type:'暂停'}
        // that.data.errorSound.push(data)
          that.setData({
            errorSoundtext:JSON.stringify(that.data.errorSound)
          })
      }
    })
    app.audioCtx.onStop(function () {
      
      console.log('歌曲停止播放')
    })
    app.audioCtx.onEnded(function () {
      console.log('歌曲自然结束播放')
    })
    app.audioCtx.onTimeUpdate(function () {
      console.log('歌曲播放进度更新播放')
    })
    app.audioCtx.onError(function (err) {
      console.log(err)
      var soundName = that.data.musicUrl.replace('https://res7.sanliwenhua.com/','')
      var data = {sound:soundName,type:'文件失败'}
      that.data.errorSound.push(data)
        that.setData({
          errorSoundtext:JSON.stringify(that.data.errorSound)
        })
      console.log('音频播放错误事件')
    })
    app.audioCtx.onWaiting(function () {
      console.log('音频加载中事件，当音频因为数据不足，需要停下来加载时会触发')
    })
    app.audioCtx.onSeeking(function () {
      console.log('音频进行 seek 操作事件')
    })
    app.audioCtx.onSeeked(function () {
      console.log('音频完成 seek 操作事件')
    })
    app.audioCtx.onPause(function () {
      console.log('歌曲暂停播放')
    })
    app.audioCtx.autoplay = true
    app.audioCtx.src = url;

    app.audioCtx.onCanplay(function () {
      // console.log('音频意外报错1')

      // that.setData({
      //   musicStart: "stat",
      //   imageStart: "imageStat",
      //   playStart: "1",
      // })
      // clearInterval(that.interval)
      // that.countDownFun()
      onCanplayFun()
    })
      console.log("我已经到了播放的方法了3")
  },
  // 用户点击右上角分享
  // onShareAppMessage: function (res) {
  //     var that = this;
  //     console.log(that)
  //     // _this.data.pic = 'http://p4svid6ld.bkt.clouddn.com/image/zhuanbi.jpg'; //测试修改
  //     if (res.from === 'button') {
  //         console.log(res)//.target
  //     }
  //     return {
  //         title: '欢乐猜歌大师',
  //         path: 'pages/index/index',
  //         success: function (res) {
  //             wx.showToast({
  //                 title: '分享成功!',
  //                 icon: 'success',
  //                 duration: 2000
  //             })
  //             console.log('分享到群的标记',res,res.shareTickets[0])
  //             app.globalData.shareTicket = res.shareTickets[0]
  //             that.fromShareBack=true;
  //             var shareNum = that.data.shareNum-1
  //             var shareNumst = that.data.shareNumst-1
  //             var mytime = new Date()
  //             var getFullYear = mytime.getFullYear().toString()
  //             var getMonth = (mytime.getMonth() + 1).toString()
  //             if (getMonth < 10) {
  //                 getMonth = "0" + (mytime.getMonth() + 1).toString()
  //             }
  //             var getDate = mytime.getDate().toString()
  //             if (getDate < 10) {
  //                 getDate = "0" + (mytime.getDate()).toString()
  //             }
  //             var date = getFullYear + getMonth + getDate
  //             console.log(date)

  //             if (app.globalData.userInfo) {
  //                 /*var shareNumber = "shareNumber" + app.globalData.userInfo.id
  //                 var sharedata = 'usreid:'+app.globalData.userInfo.id+',shareNum:'+shareNum+',time:'+date*/
  //                 //console.log("最新存储",sharedata)
  //                 if(shareNumst < 0){
  //                     shareNum = 0
  //                     that.setData({
  //                         shareNum:shareNum,
  //                         shareNumst:shareNumst
  //                     })
  //                     wx.setStorage({
  //                         key:'shareNum',
  //                         data:shareNum
  //                     })
  //                 }else{
  //                     that.setData({
  //                         shareNum:shareNum,
  //                         shareNumst:shareNumst
  //                     })
  //                     wx.setStorage({
  //                         key:'shareNum',
  //                         data:shareNum
  //                     })
  //                 }
  //                 wx.setStorage({
  //                     key:'time',
  //                     data: date
  //                 })
  //             }else{
  //                 if(shareNumst < 0){
  //                     shareNum = 0
  //                     that.setData({
  //                         shareNum:shareNum,
  //                         shareNumst:shareNumst
  //                     })
  //                     wx.setStorage({
  //                         key:'ushareNum',
  //                         data:shareNum
  //                     })
  //                 }else{
  //                     that.setData({
  //                         shareNum:shareNum,
  //                         shareNumst:shareNumst
  //                     })
  //                     wx.setStorage({
  //                         key:'ushareNum',
  //                         data:shareNum
  //                     })
  //                 }
  //                 wx.setStorage({
  //                     key:'utime',
  //                     data: date
  //                 })

  //             }
  //                 that.setData({
  //                     error: true,
  //                     overtime: true,
  //                     CountDown: 20,
  //                 })
  //         },
  //         fail: function (res) {
  //             wx.showToast({
  //                 title: '分享失败！',
  //                 image: '/image/errIcon.png',
  //                 duration: 2000
  //             })
  //         }
  //     }
  // },

  // 生命周期函数--监听页面隐藏
  onHide: function () {
    this.waveAnimationTime = 0;
    this.data.CountDown = 0;
    this.audioPause()
    // app.audioCtx.stop()
  },

  //  生命周期函数--监听页面卸载
  onUnload: function () {

  },

  //   页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {
  },

  //  * 页面上拉触底事件的处理函数
  onReachBottom: function () {

  },
  getOnlineExam: function () {
    var that = this;
    // https://res7.sanliwenhua.com/song2.json
    // wx.clearStorage()

    function sortNumber(a, b) {
      return a.id - b.id
    }
    function randemList(a, b) {
      return Math.random() > 0.5 ? 1 : -1
    }
    function saveExamTolocal(newData) {
      var strData = JSON.stringify(newData)
      wx.setStorageSync("exam", strData)
    }
    function getExamFromlocal() {
      var localExamStr = wx.getStorageSync('exam')
      if (localExamStr != '') {
        var localExam = JSON.parse(localExamStr)
        return localExam
      } else {
        return false
      }
    }

    function getFlashExam() {
      wx.request({
        url: 'https://res7.sanliwenhua.com/song2.json', //仅为示例，并非真实的接口地址
        // data: {
        //    x: '' ,
        //    y: ''
        // },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          var data = res.data.data;
          var dataItemList = []
          var newData = []
          data.map(function (item, index) {
            item.id = parseInt(item.id)
            if (index % 100 != 0) {
              dataItemList.push(item)
            } else {
              if (index > 0) {
                dataItemList.sort(randemList)
                newData.push(dataItemList)
              }
              dataItemList = [item]
            }
            return item
          })
          newData.push(dataItemList)
          // data = data.sort(sortNumber)
          // newData.sort(randemList)
            //console.log(newData)
          saveExamTolocal(newData)
          getExamFromlocal()
        },
        fail: function (res) {

        }
      })
    }
    that.examData = getExamFromlocal()
    if (!that.examData) {
      getFlashExam()
      function getNewExam() {
        var localExamIn = getExamFromlocal()
        if (!localExamIn) {
          setTimeout(function () {
              //console.log('尚未获取到远程题目')
            getNewExam()
          }, 300)

        } else {
            //console.log('已经获取到远程题目')
          that.examData = localExamIn
          that.setData({
            examData: localExamIn
          })
        }
      }
      getNewExam()
    } else {
      that.setData({
        examData: that.examData
      })
      getFlashExam()
    }
  },
  waveAnimation: function () {
    var that = this;
    // that.waveAnimationTime = setInterval(function () {
    //   that.setData({
    //     waveMove: that.data.waveMove - 1
    //   })
    //   if (that.data.waveMove < -750) {
    //     that.data.waveMove = 0
    //   }
    // }, 120)
    // that.waveAnimationTime
  }
})